<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>作者平台管理</el-breadcrumb-item>
        <el-breadcrumb-item>PC站运营</el-breadcrumb-item>
        <el-breadcrumb-item>轮播图管理</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        :table-loading="loading"
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
        @row-update="rowUpdate"
        @row-del="rowDel"
        @row-save="rowSave"
      >
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { common } from "@/api/cat/httpRequest";
import { catUrl } from "@/config/env";
import { pcConfigBanner as baseURL, uploadFile } from "@/api/cat/baseUrls";
export default {
  data() {
    return {
      loading: false,
      page: {
        total: 0,
        pageSize: 10,
      },
      type: true,
      data: [],
      params: {},
      form: {},
      default: {
        orderByList: "create_time&DESC",
      },
      option: {
        index: true,
        indexLabel: "编号",
        excelBtn: false,
        border: true,
        column: [
          {
            type: "upload",
            label: "轮播图",
            span: 24,
            display: true,
            showFileList: true,
            multiple: false,
            limit: 10,
            propsHttp: {},
            canvasOption: {},
            prop: "cover_url",
            drag: false,
            listType: "picture-img",
            fileSize: 10240,
            action: `${catUrl}${uploadFile.uploadFile}`,
            propsHttp: {
              url: "source",
            },
            rules: [
              {
                required: true,
                message: "轮播图必须上传",
              },
            ],
          },
          {
            label: "名称",
            prop: "title",
            type: "input",
            search: false,
            row: true,
            rules: [
              {
                required: true,
                message: "轮播图名称必须填写",
              },
            ],
          },
          {
            label: "跳转链接",
            prop: "jump_url",
            type: "input",
            dicData: [],
            row: true,
            rules: [
              {
                required: true,
                message: "跳转链接必须填写",
              },
            ],
          },
          {
            label: "排序",
            prop: "sort",
            type: "input",
            row: true,
            rules: [
              {
                required: true,
                message: "排序字段必须填写",
              },
            ],
          },
          {
            label: "生效日期",
            prop: "start_date",
            type: "date",
            row: true,
            format: "yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd",
          },
          {
            label: "结束日期",
            prop: "end_date",
            type: "date",
            row: true,
            format: "yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd",
          },
          {
            label: "状态",
            prop: "is_online",
            type: "select",
            hide: true,
            row: true,
            dicData: [
              {
                label: "上线",
                value: 1,
              },
              {
                label: "下线",
                value: 0,
              },
            ],
            rules: [
              {
                required: true,
                message: "状态必须填写",
              },
            ],
          },
          {
            label: "发布人",
            prop: "edit_admin_name",
            type: "input",
            addDisplay: false,
            editDisplay: false,
            addDisabled: true,
            editDisabled: true,
          },
          {
            label: "发布时间",
            prop: "edit_time",
            type: "datetime",
            addDisplay: false,
            editDisplay: false,
            addDisabled: true,
            editDisabled: true,
          },
        ],
      },
    };
  },
  methods: {
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        baseURL.pageAndParam,
        { ...this.params, ...this.default },
        this.page,
        "POST"
      );
      this.page = response.page;
      this.data = response.data;
      this.loading = false;
    },
    /**保存 */
    async rowSave(row, done, loading) {
      delete row["id"];
      let userInfo = this.$store.state.user.userInfo;
      row.edit_admin_id = userInfo.id;
      row.edit_admin_name = userInfo.username;
      let data = await common(baseURL.save, row, null, "POST");
      if (data == null) {
        loading();
        return;
      }
      this.getList();
      done();
    },
    /**修改 */
    async rowUpdate(row, index, done, loading) {
      let userInfo = this.$store.state.user.userInfo;
      row.edit_admin_id = userInfo.id;
      row.edit_admin_name = userInfo.username;
      let data = await common(baseURL.update, row, null, "POST");
      if (data == null) {
        loading();
        return;
      }
      this.getList();
      done();
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    /**删除 */
    rowDel(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        type: "warning",
      })
        .then(async () => {
          let response = await common(
            baseURL.delete,
            { id: row.id },
            null,
            "GET"
          );
        })
        .then(() => {
          this.getList();
        });
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    /**搜索 */
    searchChange(params, done) {
      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },
  },
};
</script>